/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MultipleNodeEditor.java
 *
 * Created on 2011-12-08, 22:18:11
 */
package pobicos;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;

/**
 *
 * @author marcin
 */
public class MultipleNodeEditor extends javax.swing.JFrame {

    /** Creates new form MultipleNodeEditor */
    public MultipleNodeEditor(final JungTreePanel graphicTree) {
        this.setLocationRelativeTo(graphicTree.getMainViewFrame());
        initComponents();
        enterButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {            
                MyNode<Agent> tempPickedNode = graphicTree.getLogicalTree().getNodeAt(graphicTree.currentPickedVertex);
                try {
                    Integer n = Integer.parseInt(multipleTextField.getText());
                    if (tempPickedNode.getNumberOfChildren() != 0) {
                                                dispose();
                        JOptionPane.showMessageDialog(null, "Only leaf agents can be set to multiple mode.");
                    } else {
                    /**
                     * Set multiple mode to current agent
                     */
                    tempPickedNode.getData().setMultipleMode(n);
                    tempPickedNode.getData().setInstacesHappMap(new HashMap<Integer, Map<String, Double>>() );
                    /**
                     * Add current agent to multiple mode collection
                     */
                    graphicTree.getMultipleNodeCollection().add(graphicTree.currentPickedVertex);
                    }
                    
                } catch (NumberFormatException w) {
                    AdaptSysView.isMultipleCheckBox.setSelected(false);
                    JOptionPane.showMessageDialog(null, "Incorrect number format!");
                }
                graphicTree.getPickedState().clear();
                dispose();

            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        enterButton = new javax.swing.JButton();
        multipleTextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pobicos.AdaptSysApp.class).getContext().getResourceMap(MultipleNodeEditor.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        enterButton.setText(resourceMap.getString("enterButton.text")); // NOI18N
        enterButton.setName("enterButton"); // NOI18N

        multipleTextField.setText(resourceMap.getString("multipleTextField.text")); // NOI18N
        multipleTextField.setName("multipleTextField"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(enterButton)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(multipleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(multipleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(enterButton))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton enterButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField multipleTextField;
    // End of variables declaration//GEN-END:variables
}
